package com.sc.auction.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.auction.common.exception.AuctionException;
import com.sc.auction.common.util.R;
import com.sc.auction.common.util.ResultCode;
import com.sc.auction.user.pojo.Address;
import com.sc.auction.user.service.AddressService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author theTi
 * @since 2020-09-01
 */
@RestController
@RequestMapping("/user/address")
@CrossOrigin
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation("增加地址")
    @PostMapping(value = "/addAddress")
    public R addAddress(@RequestBody Address address) throws AuctionException {
        boolean flag=false;
        if(address!=null){
            flag=addressService.save(address);
        }
        if(flag){
            return R.ok().code(200).message("添加成功!");
        }else {
            throw new AuctionException(201,"增加失败!");
        }
    }


    @PutMapping(value = "/updateAddress")
    @ApiOperation("根据id修改地址信息")
    public R updateAddress(@RequestBody Address address) {
        boolean flag=false;
        if (address != null && address.getId() != null) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id", address.getId());
            flag = addressService.update(address, queryWrapper);
        }
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @ApiOperation("根据id删除地址")
    @DeleteMapping(value = "/deleteAddressById")
    public R deleteAddressById(Integer id){
        boolean flag=false;
        if(id!=null&&id!=0){
            flag=addressService.removeById(id);
        }
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping(value = "/getAddressById")
    @ApiOperation("根据用户id查询地址信息")
    public R getAddressById(Integer userid)throws AuctionException{
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userid",userid);
        List <Address> addressList = addressService.list(queryWrapper);
        if(addressList!=null){
            return R.ok().data("address",addressList);
        }else {
            throw new AuctionException(ResultCode.ERROR,"查询失败");
        }
    }

    @GetMapping(value = "/findAllAddress")
    @ApiOperation("查询所有地址信息或根据用户id查询")
    public R findAllAddress(@ApiParam(name = "page",defaultValue = "1",required = true)@RequestParam("page") Integer page,
                         @ApiParam(name = "limit",defaultValue = "5",required = true)@RequestParam("limit") Integer limit,
                         Integer id) throws AuctionException{
        Page<Address> pageBean=new Page<>(page,limit);
        QueryWrapper queryWrapper=null;
        if(id!=null) {
            queryWrapper = new QueryWrapper();
            if(id!=0&&id<0){
                queryWrapper.eq("userid",id);
            }
        }
        addressService.page(pageBean,queryWrapper);
        List<Address> records = pageBean.getRecords();
        long total = pageBean.getTotal();

        if (records!=null&&records.size()>0){
            return R.ok().data("total",total).data("records",records);
        }else {
            throw  new AuctionException(ResultCode.ERROR,"未查找到用户信息");
        }
    }

    @GetMapping(value = "/getAddressById1")
    @ApiOperation("根据id查询地址信息")
    public R getAddressById1(Integer id)throws AuctionException{
        Address address = addressService.getById(id);
        if(address!=null){
            return R.ok().data("address",address);
        }else {
            throw new AuctionException(ResultCode.ERROR,"查询失败");
        }
    }
}

